{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10个问题\n",
    "1.输入某年某月某日，判断这一天是这一年的第几天？  \n",
    "2.判断1000中有多少个素数，并输出所有素数。  \n",
    "3.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）  \n",
    "4.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。  \n",
    "5.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。  \n",
    "6.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）   \n",
    "7.输入任意奇数N，按如下要求打印出图案（菱形）  \n",
    "8.对10个数进行排序。  \n",
    "9.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中  \n",
    "10.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在执行代码块5\n",
      "1000以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "# 定义代码块对应的函数\n",
    "def code_block_1():\n",
    "    print(\"正在执行代码块1\")\n",
    "    import calendar\n",
    "    year=int(input(\"请输入年份: \"))\n",
    "    month=int(input(\"请输入月份: \"))\n",
    "    day=int(input(\"请输入日期: \"))\n",
    "    def day_of_year_using_calendar(year, month, day):\n",
    "        day_count = 0\n",
    "        for m in range(1, month):  \n",
    "            _, days_in_month = calendar.monthrange(year, m)\n",
    "            day_count += days_in_month\n",
    "            day_count += day \n",
    "        return day_count\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{day_of_year_using_calendar(year, month, day)}天\")\n",
    "    # 在这里添加代码块1的具体逻辑\n",
    "\n",
    "def code_block_2():\n",
    "    print(\"正在执行代码块2\")\n",
    "    def is_prime(n, primes):\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        for prime in primes:\n",
    "            if prime * prime > n:\n",
    "                break  \n",
    "            if n % prime == 0:\n",
    "                return False  \n",
    "        return True  \n",
    "\n",
    "    def find_primes_up_to(limit):\n",
    "        primes = [] \n",
    "        for num in range(2, limit + 1):\n",
    "            if is_prime(num, primes):\n",
    "                primes.append(num) \n",
    "        return primes\n",
    "    limit = 1000\n",
    "    primes = find_primes_up_to(limit) \n",
    "    print(f\"1000以内的素数个数为: {len(primes)}\")\n",
    "    print(\"1000以内的所有素数为:\")\n",
    "    for prime in primes:\n",
    "        print(prime, end=' ')\n",
    "        \n",
    "\n",
    "def code_block_3():\n",
    "    print(\"正在执行代码块3\")\n",
    "    score = float(input(\"请输入学生的成绩：\"))\n",
    "    grade = 'A' if score >= 90  else ('B' if score >= 60 else 'C')\n",
    "    print(\"学生的成绩等级是：\", grade)\n",
    "    \n",
    "    \n",
    "def code_block_4():\n",
    "    print(\"正在执行代码块4\")\n",
    "    def count_characters(s):\n",
    "        letters = sum(1 for c in s if c.isalpha())\n",
    "        spaces = sum(1 for c in s if c.isspace())\n",
    "        digits = sum(1 for c in s if c.isdigit())\n",
    "        others = sum(1 for c in s if not c.isalnum() and not c.isspace())\n",
    "        return letters, spaces, digits, others\n",
    "\n",
    "    input_string = input(\"请输入一行字符串：\")\n",
    "    letters, spaces, digits, others = count_characters(input_string)\n",
    "    \n",
    "    print(f\"英文字母个数：{letters}\")\n",
    "    print(f\"空格个数：{spaces}\")\n",
    "    print(f\"数字个数：{digits}\")\n",
    "    print(f\"其他字符个数：{others}\")\n",
    "\n",
    "    \n",
    "def code_block_5():\n",
    "    print(\"正在执行代码块5\")\n",
    "    def find_perfect_numbers(n):\n",
    "        perfect_numbers = []  \n",
    "        for num in range(1, n+1):\n",
    "            factors_sum = 0  \n",
    "            for i in range(1, num):\n",
    "                if num % i == 0:  \n",
    "                    factors_sum += i  \n",
    "            if factors_sum == num:  \n",
    "                perfect_numbers.append(num)  \n",
    "        return perfect_numbers\n",
    "    perfect_numbers = find_perfect_numbers(1000)\n",
    "    print(\"1000以内的完数有：\", perfect_numbers)\n",
    "    \n",
    "    \n",
    "def code_block_6():\n",
    "    print(\"正在执行代码块6\")\n",
    "    def factorial(n):\n",
    "        if n == 0:\n",
    "            return 1\n",
    "        else:\n",
    "            return n * factorial(n-1)\n",
    "\n",
    "    N = int(input(\"请输入一个正整数N：\"))\n",
    "    def sum_of_factorials(N):\n",
    "        sum = 1  \n",
    "        for i in range(2, N+1):\n",
    "            sum += factorial(i)\n",
    "        return sum\n",
    "    print(f\"1 + 2! + 3! + ... + {N}! 的值是：{sum_of_factorials(N)}\")\n",
    "\n",
    "\n",
    "def code_block_7():\n",
    "    print(\"正在执行代码块7\")\n",
    "    N = int(input(\"请输入一个奇数N：\"))\n",
    "    def print_diamond(N):\n",
    "        if N % 2 == 0:\n",
    "            print(\"请输入一个奇数\")\n",
    "            return\n",
    "        for i in range(N // 2 + 1):\n",
    "            print(' ' * (N // 2 - i) + '*' * (2 * i + 1))\n",
    "        for i in range(N // 2 - 1, -1, -1):\n",
    "            print(' ' * (N // 2 - i) + '*' * (2 * i + 1))\n",
    "    print_diamond(N)\n",
    "\n",
    "\n",
    "def code_block_8():\n",
    "    print(\"正在执行代码块8\")\n",
    "    def get_numbers():\n",
    "        numbers = []\n",
    "        print(\"请输入10个数，每输入一个数后按回车键：\")\n",
    "        for i in range(10):\n",
    "            while True:\n",
    "                try:\n",
    "                    num = float(input(f\"输入第{i+1}个数：\"))\n",
    "                    numbers.append(num)\n",
    "                    break\n",
    "                except ValueError:\n",
    "                    print(\"输入无效，请输入一个数字。\")\n",
    "        return numbers\n",
    "\n",
    "    def sort_numbers(numbers):\n",
    "        return sorted(numbers)\n",
    "    def main():\n",
    "        numbers = get_numbers()\n",
    "        sorted_numbers = sort_numbers(numbers)\n",
    "        print(\"输入的数列:\", numbers)\n",
    "        print(\"排序后的数列:\", sort_numbers(numbers))\n",
    "    if __name__ == \"__main__\":\n",
    "        main()\n",
    "    \n",
    "    \n",
    "def code_block_9():\n",
    "    print(\"正在执行代码块9\")\n",
    "    def binary_search_insert(arr, target):\n",
    "        low, high = 0, len(arr) - 1\n",
    "        while low <= high:\n",
    "            mid = (low + high) // 2\n",
    "            if arr[mid] == target:\n",
    "                return mid \n",
    "            elif arr[mid] < target:\n",
    "                low = mid + 1\n",
    "            else:\n",
    "                high = mid - 1\n",
    "        return low  \n",
    "\n",
    "    def insert_into_sorted_array(arr, target):\n",
    "        index = binary_search_insert(arr, target)\n",
    "        arr.insert(index, target)\n",
    "        return arr\n",
    "    \n",
    "    sorted_array = [1, 4, 8, 56, 545, 879, 999, 4266, 45645, 4562425]  \n",
    "    number_to_insert = int(input(\"请输入一个数：\"))  \n",
    "    new_array = insert_into_sorted_array(sorted_array, number_to_insert)\n",
    "    print(\"插入后的数组：\", new_array)\n",
    "    \n",
    "    \n",
    "def code_block_10():\n",
    "    print(\"正在执行代码块10\")\n",
    "    def last_person_standing(n):\n",
    "        people = list(range(1, n+1))\n",
    "        index = 0\n",
    "        while len(people) > 1:\n",
    "            index = (index + 2) % len(people)\n",
    "            people.pop(index)\n",
    "        return people[0]\n",
    "\n",
    "    n = int(input(\"请输入一个数字n：\"))\n",
    "    print(\"最后留下的是原来第{}号的那位。\".format(last_person_standing(n)))\n",
    "   \n",
    "    \n",
    "\n",
    "user_input = input(\"请输入数字来选择要运行的代码块：\")\n",
    "\n",
    "if user_input == '1':\n",
    "    code_block_1()\n",
    "elif user_input == '2':\n",
    "    code_block_2()\n",
    "elif user_input == '3':\n",
    "    code_block_3()\n",
    "elif user_input == '4':\n",
    "    code_block_4()\n",
    "elif user_input == '5':\n",
    "    code_block_5()\n",
    "elif user_input == '6':\n",
    "    code_block_6()\n",
    "elif user_input == '7':\n",
    "    code_block_7()\n",
    "elif user_input == '8':\n",
    "    code_block_8()\n",
    "elif user_input == '9':\n",
    "    code_block_9()\n",
    "elif user_input == '10':\n",
    "    code_block_10()\n",
    "else:\n",
    "    print(\"输入无效，请输入1-10。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
